package com.itheima.service.pmsService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.PageBean;
import com.itheima.domain.db.pms.PmsProductAttribute;
import com.itheima.domain.db.pms.PmsProductCategoryAttributeRelation;

import com.itheima.mapper.pmsMapper.PmsProductCategoryAttributeRelationMapper;
import com.itheima.mapper.pmsMapper.ProductAttributeMapper;
import com.itheima.service.pmsService.ProductAttributeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class ProductAttributeServiceImpl implements ProductAttributeService {

    @Autowired
    ProductAttributeMapper productAttributeMapper;

    @Autowired
    PmsProductCategoryAttributeRelationMapper ppcarMapper;

    @Override
    public void addProductAttribute(PmsProductAttribute pmsProductAttribute) {
        productAttributeMapper.insert(pmsProductAttribute);
    }

    @Override
    public void deleteProductAttribute(Integer[] ids) {
        QueryWrapper<PmsProductAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        productAttributeMapper.delete(queryWrapper);
    }

    @Override
    public PageBean<PmsProductAttribute> listCid(Integer cid, Integer pageNum, Integer pageSize, Integer type) {

        Page<PmsProductAttribute> pagInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,cid);
        wrapper.eq(PmsProductAttribute::getType,type);
        Page<PmsProductAttribute> productAttributePage = productAttributeMapper.selectPage(pagInfo, wrapper);

        PageBean<PmsProductAttribute> pmsProductAttributePageBean = new PageBean<>(pagInfo);
        return pmsProductAttributePageBean;
    }

    @Override
    public void updateProductAttribute(Integer id, PmsProductAttribute pmsProductAttribute) {
        LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(PmsProductAttribute::getId, id);

        productAttributeMapper.update(pmsProductAttribute, queryWrapper);
    }

    @Override
    public List<PmsProductCategoryAttributeRelation> getById(Integer productCategoryId) {

        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategoryAttributeRelation::getId,productCategoryId);
        List<PmsProductCategoryAttributeRelation> ppcarList = ppcarMapper.selectList(lqw);

        return ppcarList;
    }

    @Override
    public PmsProductAttribute getProductAttrById(Integer id) {
        return productAttributeMapper.selectById(id);
    }
}
